#coding:utf-8
'''
create on 2017-04-20

@author: sandy
'''
import sys,platform
sys.path.append("../")
from twisted.internet import reactor
from pycommon import Daemon
from pycommon import LogManager
#from pycommon import RedisDb
from pycommon import Const
from pycommon import Utils
from pycommon import RemoteManager
from pycommon import LogServer
import CenterWebSite
import GateFactory
import GameFactory
import LoginFactory
import logging
import ServerConfig
import TelnetServer
import GameDb
import CounterManager
import GiveBackCardConfig
import PropConfig
import FirstLoginConfig
import CreateTableConfig
import GateManager
import BillFactory
import CenterCtxManager

if 'twisted.internet.reactor' not in sys.modules: 
    if platform.system() == "Linux":
        from twisted.internet import epollreactor
        epollreactor.install()  
    else:
        from twisted.internet import iocpreactor
        iocpreactor.install()

def ServerStart(isdaemon):
    logging.info("############Start CenterServer############")
    CounterManager.instance.start(Const.COUNTER_CENTER_SAVE_INTERVAL)
    GateManager.instance.start(Const.COUNTER_CENTER2LOIN_INTERVAL)
    CenterCtxManager.instance.start()
    GameDb.instance.start(ServerConfig.instance.game_db_name,ServerConfig.instance.game_db_user,ServerConfig.instance.game_db_passwd,ServerConfig.instance.game_db_host,ServerConfig.instance.game_db_port,ServerConfig.instance.game_db_linkcount)
    CenterWebSite.instance.start(ServerConfig.instance.web_ip,ServerConfig.instance.web_port)
    TelnetServer.instance.start(ServerConfig.instance.telnet_ip,ServerConfig.instance.telnet_port)
    GateFactory.instance.start(ServerConfig.instance.server_ip,ServerConfig.instance.server_port_gate,ServerConfig.instance.max_client)
    GameFactory.instance.start(ServerConfig.instance.server_ip,ServerConfig.instance.server_port_game,ServerConfig.instance.max_client)
    BillFactory.instance.start(ServerConfig.instance.server_ip,ServerConfig.instance.server_port_bill,ServerConfig.instance.max_client)
    LoginFactory.instance.start(ServerConfig.instance.server_ip,ServerConfig.instance.server_port_login,ServerConfig.instance.max_client)
    LogServer.instance.start(ServerConfig.instance.log_ip,ServerConfig.instance.log_port)


def ServerStop():
    LoginFactory.instance.stop()
    BillFactory.instance.stop()
    GameFactory.instance.stop()
    GateFactory.instance.stop()
    TelnetServer.instance.stop()
    CenterWebSite.instance.stop()
    GameDb.instance.stop()

def getConfig(isdaemon):
    RemoteManager.instance.addConfigCallback(GiveBackCardConfig.instance)
    RemoteManager.instance.addConfigCallback(PropConfig.instance)
    RemoteManager.instance.addConfigCallback(FirstLoginConfig.instance)
    RemoteManager.instance.addConfigCallback(CreateTableConfig.instance)
    configlist = RemoteManager.instance.getConfigList()
    RemoteManager.instance.requestConfig(configlist,ServerStart,isdaemon)

def getServerConfig(isdaemon):
    configlist = [u'server%d.*' % RemoteManager.instance.sid]
    RemoteManager.instance.addConfigCallback(ServerConfig.instance)
    RemoteManager.instance.requestConfig(configlist,getConfig,isdaemon)    

def daemonFunc(isdaemon):
    LogManager.instance.setupLog(RemoteManager.instance.loglevel,RemoteManager.instance.logfile,isdaemon)
    logging.info("############Update CenterServer Config############")
    RemoteManager.instance.start(getServerConfig,isdaemon)
    reactor.run()
    ServerStop()

def run():
    sid, msip, msport, pidpath, logfile, loglevel, pathSelfConfig = Utils.loadDaemonConfig(ServerConfig.instance.rundir)
    RemoteManager.instance.init(sid,msip,msport,logfile, loglevel)
    Daemon.run(pidpath, daemonFunc)

if __name__ == "__main__":
    run()